<?php
// require('../config/mysql.php');
    class User {
        // var $value;
    
        /* 登录v1.0.0 */
        // 账号
        // 密码
        // mysql配置
        function User_09051($username, $password, $conn)
        {
            // 检测账号密码
            $result = $conn->query("SELECT * FROM user WHERE username=$username AND password=$password ");
            if ($result->rowCount() == 1) {
                // 检测成功获取账号信息
                foreach($result->FetchAll() as $result) {
                    $id =  $result['id'];
                    $level = $result['level'];
                }
                if ($id and $level) {
                    // 写一条token到数据库
                    $token = $this->User_09061($id, $level, $conn);
                } else {
                    $token = false;
                }
            } else {
                $token = false;
            }
            
            return $token;
        }
        
        /* 生成临时令牌v1.0.0 */
        // 用户ID
        // 给与令牌的等级
        // mysql配置
        function User_09061($id, $level, $conn) {
            // 删除旧令牌
            $conn->exec("DELETE FROM `token` WHERE `id`='$id'");
            // 延迟0.3妙
            usleep(300000);
            // 生成MD5令牌
            $time = intval(time());
            $token = md5($id.$level.$time);
            // 写一条token到数据库
            $conn->exec("INSERT INTO token (id, token, time, end, level) VALUES ('$id', '$token', '$time', $time+1800, '$level')");
            // 检测插入状况
            $result = $conn->query("SELECT * FROM `token` WHERE `id` = $id AND `token` = '$token' ");
            foreach($result->FetchAll() as $result) {
                $token2 = $result['token'];
            }
            // 检测结果
            if (!$token2) {
                $token = false;
            }

            return $token;
        }

        /* 用户注册 */
        // 注册的用户账户
        // 注册的用户密码
        // 绑定的邮箱
        // 验证码
        // mysql配置
        function User_09062($username, $password, $email, $code, $conn)
        {
            // 生成一条ID
            $id = rand(200000,999999);
            // 检测账户
                // 账号
                $result1 = $conn->query("SELECT * FROM `user` WHERE `username` = $username ");
                // 邮箱
                $result2 = $conn->query("SELECT * FROM `user` WHERE `email` = '$email' ");
                // id
                $result3 = $conn->query("SELECT * FROM `user` WHERE `id` = $id ");
            // 检测账号和邮箱
            if ($result1->rowCount() <1 and $result2->rowCount() <1) {
                // 账号和邮箱之前都没有被注册过
                if ($result3->rowCount() <1) {
                    // 校验验证码
                    $code = $this->User_09063($code, $email, $conn);
                    if ($code) {
                        // 写入一条用户数据
                        $time =  date('Y-m-d H:i:s');
                        $conn->exec("INSERT INTO `user` (`id`, `username`, `password`, `email`, `pay_password`, `level`, `reg_time`, `ip`) VALUES ('$id', '$username', '$password', '$email', '123456', '10', '$time', '127.0.0.1')");
                        // 登录
                        $token = $this->User_09051($username, $password, $conn);
                        //$this->User_09071($email, $conn);
                    } else {
                        // 验证码验证失败
                        $token = false;
                    }
                } else {
                    // ID值重复，重新加载数据进行注册，直至通过为止
                    $token = $this->User_09062($username, $password, $email, $conn);
                }
            } else {
                // 之前有被注册过
                $token = false;
            }

            return $token;
        }
        
        /* 验证码校验 */
        // 验证码
        // 邮箱号
        // mysql配置
        // 用户ID
        function User_09063($code, $email, $conn, $id = 100000)
        {
            // SELECT * FROM `code` WHERE `id` = $id AND `code` = $code AND `type` = 1 AND `email` = $email 
            $result = $conn->query("SELECT * FROM code WHERE `id` = $id AND `code` = $code AND `type` = 1 AND `email` = '$email' ");
            if ($result->rowCount() == 1) {

                // 判断是否过期，是否可用
                foreach($result->FetchAll() as $result) {
                    $time = $result['time'];
                    $end = $result['end'];
                    $type = $result['type'];
                }
                $time2 = time();
                if ($type == 1 and $time2 < $end) {
                    // 验证码可用
                    $conn->exec("UPDATE `code` SET `type` = '0' WHERE `code`.`id` = $id AND `code`.`time` = $time AND `code`.`end` = $end ");
                    $value = true;
                } else {
                    // 验证码存在，但已使用或过期
                    $value = false;
                }


            } else {
                $value = false;
            }

            return $value;
        }

        /* 发送邮件验证码 */

        // 验证码拥有者
        // 收件人邮箱
        // mysql配置
        // 邮件类型
        // 验证码过期时间
        function User_09071($addressee = "domycat@yeah.net", $conn, $id = 100000, $body = false, $time = 600)
        {
            // 生成一条验证码
            $code = rand(100000,999999);
            // 检测验证码有没有存在
            $result = $conn->query("SELECT * FROM `code` WHERE `id` = $id AND `code` = $code ");
            if ($result->rowCount() <1) {
                // 在数据库写一条验证码，并且发送邮件
                $times = time();
                $time += $times;

                $conn->exec("INSERT INTO `code` (`id`, `code`, `time`, `end`, `type`, `email`) VALUES ('$id', '$code', '$times', '$time', '1', '$addressee') ");

                // 配置
                $config = array(
                    // 邮件
                    'Host' => 'smtp.qq.com',
                    'Username' => '42964179@qq.com',
                    'Password' => 'kklfiosfxriibibb',
                    'title' => 'DPC在线商城',
                    'body' => $body?$bode:'验证码：'.$code.'，该验证码10分钟内有效。为了保障您的账户安全，请勿向他人泄漏验证码信息。',
                    'addAddress' => $addressee, // 收件人
                );

                require('../plugins/email/mail.php');

            } else {
                // 验证码存在过了，那就重新加载
                $this->User_09071($addressee, $conn, $id, $body, $time);
            }

            return $code;

        }

        /* 发送邮件 or 发送邮件验证码 v2.0.0 */
        // 收件人
        // mysql配置
        // 验证码ID
        // 邮件类型
        // 邮件自定义信息
        // 验证码时间
        function User_09081($addressee, $conn, $id = 100000, $type = true, $body = false, $time = 600) {
            // 检验发送的邮件信息类型
            if ($type) {
                // 生成一条验证码
                $code = rand(100000,999999);
                $result = $conn->query("SELECT * FROM `code` WHERE `id` = $id AND `code` = $code ");
                if ($result->rowCount() <1) {
                    // 在数据库写一条验证码，并且发送邮件
                    $times = time();
                    $time += $times;
                    $conn->exec("INSERT INTO `code` (`id`, `code`, `time`, `end`, `type`, `email`) VALUES ('$id', '$code', '$times', '$time', '1', '$addressee') ");
                    // 配置
                    $config = array(
                        // 邮件
                        'Host' => 'smtp.qq.com',
                        'Username' => '42964179@qq.com',
                        'Password' => 'kklfiosfxriibibb',
                        'title' => 'DPC在线商城',
                        'body' => '验证码：'.$code.'，该验证码10分钟内有效。为了保障您的账户安全，请勿向他人泄漏验证码信息。',
                        'addAddress' => $addressee, // 收件人
                    );

                    require('../plugins/email/mail.php');

                } else {
                    // 验证码存在过了，那就重新加载
                    $this->User_09081($addressee, $conn, $id);
                }
            } else {
                // 发送自定义邮件
                // 配置
                $config = array(
                    // 邮件
                    'Host' => 'smtp.qq.com',
                    'Username' => '42964179@qq.com',
                    'Password' => 'kklfiosfxriibibb',
                    'title' => 'DPC在线商城',
                    'body' => $body,
                    'addAddress' => $addressee, // 收件人
                );

                require('../plugins/email/mail.php');
            }

            return $code;
        }

        /* 检验token */
        // 令牌
        // mysql配置
        function User_09082($token, $conn) {
            $result = $conn->query("SELECT * FROM `token` WHERE `token` = '$token' ");
            if ($result->rowCount() == 1) {
                // 获取值
                foreach($result->FetchAll() as $result) {
                    // $time = $result['time'];
                    $end = $result['end'];
                    $id = $result['id'];
                    $level = $result['level'];
                }
                if (time() < $end) {
                    $value = array(
                        'id' => $id,
                        'level' => $level,
                    );
                } else {
                    $value = false;
                }

            } else {
                $value = false;
            }

            return $value;
        }

        // 通过token获取用户信息
        function User_09111($token, $conn) {
            // 校验令牌，并获取到ID
            $result = $this->User_09082($token,$conn);
            if ($result['id']) {
                $id = $result['id'];
                // 获取用户信息
                $result = $conn->query("SELECT * FROM `info` WHERE `id` = $id ");
                if ($result->rowCount() == 1) {
                    foreach($result->FetchAll() as $result) {
                        $name = $result['name'];
                        $photo = $result['photo'];
                    }
                    $arr = array(
                        'id' => $id,
                        'name' => $name,
                        'photo' => $photo
                    );
                } else {
                    $arr = false;
                }
            } else {
                $arr = false;
            }


            return $arr;
        }

    }
?>